<?PHP
/* $Id: sync.php 7 2009-03-24 20:25:52Z ronan $ */

include 'includes/config.php';
include 'includes/database.php';
include 'includes/library.php';

require_once('api/class.api.php');
require_once('api/class.charselect.php');
require_once('api/class.membertrack.php');

/*** SETUP API ***/
$api = new Api();
$api->setCredentials($directorA->id,$directorA->apiKey,$directorA->charID);
//$api->setCredentials($aid, $uapi, $name);
//$api->setCredentials("userid", "apikey", "charid");
$api->debug(true);
$api->cache(false);

$memberTrack = new MemberTrack();
$contents = $api->getMemberTracking();
$members = $memberTrack->getMembers($contents);

/*** EMPTY TEMP TABLE ***/
$query = "TRUNCATE TABLE tempChars";
//if (mysql_query($query)) echo "Temporary table emptied...<br>";
//this might be it?
mysql_query($query);

/*** ADD CHARACTERS TO TEMP TABLE ***/
$index = 0;
$query = "INSERT INTO tempChars (charID, arrayIndex) VALUES";
foreach ($members as $member) {
	if ($index != 0) $query .= ",";
	$query .= "({$member['characterID']}, {$index})";
	$index++;
}
$results = mysql_query($query);
//if ($results) echo "API added to temporary table...<br>";

/*** LOOK FOR NEW EX-MEMBERS ***/
$query = "SELECT characters.charID, characters.name FROM characters
	LEFT JOIN tempChars ON characters.charID = tempChars.charID
	WHERE characters.inCorp = 1
	AND tempChars.charID IS NULL";
$query2 = "SELECT tempChars.charID FROM tempChars";
$results = mysql_query($query2);
$temp2 = 0;
while($temp1 = mysql_fetch_array($results))
	if(!empty($temp1['charID'])) $temp2 = 1;
$results = mysql_query($query);

if($temp2 == 1)
{
	while ($char = mysql_fetch_array($results)) {
		$time = eveTime();
		
		$newsQuery = "INSERT INTO news (dateTime, charID, type)
			VALUES ({$time}, {$char['charID']}, 1)";
		mysql_query($newsQuery);
		
		$updateQuery = "UPDATE characters SET inCorp = 0 WHERE charID = {$char['charID']}";
		mysql_query($updateQuery);
		//deleting from the temp table
		mysql_query("DELETE FROM caps WHERE charID = '".$char['charID']."'");
		
		//echo "{$char['name']} left corp...<br>";
	}	
}

/*** LOOK FOR NEW FORMER MEMBERS ***/
$query = "SELECT * FROM tempChars";
$results = mysql_query($query);
$time = eveTime();

while ($char = mysql_fetch_array($results)) {
	$query2 = "SELECT charID FROM characters WHERE charID = {$char['charID']} AND inCorp = 0";
	$results2 = mysql_query($query2);
	if (mysql_num_rows($results2) > 0) {
		$member = $members[$char['arrayIndex']];	
		$dateJoined = strtotime($member['startDateTime']);
		$updateQuery = "UPDATE characters SET inCorp = 1, joined = {$dateJoined} WHERE charID = {$char['charID']}";
		mysql_query($updateQuery);
		$newsQuery = "INSERT INTO news (dateTime, charID, type)
			VALUES ({$time}, {$char['charID']}, 2)";
	}
}

/*** LOOK FOR NEW MEMBERS ***/
$query = "SELECT tempChars.arrayIndex, tempChars.charID FROM tempChars
	LEFT JOIN characters ON tempChars.charID = characters.charID
	WHERE characters.charID IS NULL";
$results = mysql_query($query);

//$time = eveTime();

while ($char = mysql_fetch_array($results)) {
	$newsQuery = "INSERT INTO news (dateTime, charID, type)
		VALUES ({$time}, {$char['charID']}, 2)";
	mysql_query($newsQuery);
	
	$member = $members[$char['arrayIndex']];	
	$name = addslashes($member['name']);
	$lastSeen = strtotime($member['logoffDateTime']);
	$dateJoined = strtotime($member['startDateTime']);
	$updateQuery = "INSERT INTO characters (charID, realID, name, lastSeen, joined)
		VALUES ({$member['characterID']}, {$member['characterID']}, '{$name}', {$lastSeen}, {$dateJoined})";
	mysql_query($updateQuery);
	//updating the temp list as well
	//mysql_query("INSERT INTO caps VALUES('".$member['characterID']."','".$name."','','','','')");
		
	//echo "{$name} joined the corp...<br>";
}

/*** UPDATE EXISTING MEMBERS ***/
$query = "SELECT tempChars.arrayIndex FROM characters, tempChars
	WHERE characters.charID = tempChars.charID
	AND characters.inCorp = 1";
$results = mysql_query($query) or die(mysql_error());

while ($char = mysql_fetch_array($results)) {
	$idx = $char['arrayIndex'];
	$member = $members[$idx];	
	$lastSeen = strtotime($member['logoffDateTime']);
	$dateJoined = strtotime($member['startDateTime']);
	$updateQuery = "UPDATE characters SET lastSeen = {$lastSeen}, joined = {$dateJoined}
		WHERE charID = {$member['characterID']}";
	mysql_query($updateQuery);
	
		//echo "{$member['name']} has been updated...<br>";
}

/*** AUTO-FLAG INACTIVE ***/
$time30daysago = eveTime() - 60*60*24*30;
$query = "SELECT charID, name FROM characters
	WHERE lastSeen < {$time30daysago}
	AND inactive != 1
	AND inCorp = 1";
$results = mysql_query($query);

$time = eveTime();

while ($char = mysql_fetch_array($results)) {
	$newsQuery = "INSERT INTO news (dateTime, charID, type)
		VALUES ({$time}, {$char['charID']}, 3)";
	mysql_query($newsQuery);
	
	//echo "{$char['name']} is inactive...<br>";
}

$updateQuery = "UPDATE characters SET inactive = 1
	WHERE lastSeen < {$time30daysago}";
mysql_query($updateQuery);

/*** AUTO-UNFLAG INACTIVE ***/
$query = "SELECT charID, name FROM characters
	WHERE lastSeen >= {$time30daysago}
	AND inactive = 1
	AND inCorp = 1";
$results = mysql_query($query);

while ($char = mysql_fetch_array($results)) {
	$newsQuery = "INSERT INTO news (dateTime, charID, type)
		VALUES ({$time}, {$char['charID']}, 5)";
	mysql_query($newsQuery);
	
	//echo "{$char['name']} is active...<br>";
}

$updateQuery = "UPDATE characters SET inactive = 0
	WHERE lastSeen >= {$time30daysago}
	AND inactive = 1";
$results = mysql_query($updateQuery);

mysql_query("DELETE FROM time WHERE updateTIME > 0");
mysql_query("INSERT INTO time values('".time()."')");
?>
